﻿using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Microsoft.EntityFrameworkCore;
using Mt.Site.Application.Dto;
using Mt.Site.Application.Modules.Dto;
using Mt.Site.Core.Navigations.Entities;
using Mt.Site.Core.Navigations.Modules;
using Mt.Site.Core.Navigations.Permissions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Mt.Site.Application.Modules
{
    [AbpAuthorize(MenuPermissions.Pages_Administration_FrontendPageView)]
    public class PageViewAppService : MtSiteAppServiceBase, IPageViewAppService
    {
        private readonly IPageViewManger _pageViewManager;
        private readonly IRepository<PageView, Guid> _pageViewRepository;

        public PageViewAppService(
            IPageViewManger pageViewManager,
            IRepository<PageView, Guid> pageViewRepository)
        {
            _pageViewManager = pageViewManager;
            _pageViewRepository = pageViewRepository;
        }

        [AbpAuthorize(MenuPermissions.Pages_Administration_FrontendPageView_Create)]
        public async Task CreateAsync(CreatePageViewDto input)
        {
            var m = ObjectMapper.Map<PageView>(input); 
            await _pageViewManager.CreateAsync(m);
        }

        [AbpAuthorize(MenuPermissions.Pages_Administration_FrontendPageView_Edit)]
        public async Task UpdateAsync(UpdatePageViewDto input)
        {
            var m = await _pageViewManager.GetAsync(input.Id);

            m.IsActive = input.IsActive;
            m.Name = input.Name;
            m.PageSize = input.PageSize;
            m.BodyView = input.BodyView;
            
            m.PcOrMobile = input.PcOrMobile;
            m.FrontEndPageId = input.FrontEndPageId; 

            await _pageViewManager.UpdateAsync(m);
        }

        [AbpAuthorize(MenuPermissions.Pages_Administration_FrontendPageView_Delete)]
        public async Task DeleteAsync(EntityDto<Guid> input)
        {
            await _pageViewManager.DeleteAsync(input.Id);
        }

        [AbpAuthorize(MenuPermissions.Pages_Administration_FrontendPageView_Delete)]
        public async Task DeleteListAsync(List<Guid> input)
        {
            await _pageViewManager.DeleteListAsync(input);
        }

        public async Task<GetPageViewForEditOutput> GetForEditAsync(NullableIdDto<Guid> input)
        {
            var dto = new GetPageViewForEditOutput();
            if (input.Id.HasValue)
            {
                var page = await _pageViewManager.GetAsync(input.Id.Value);
                dto.PageView = ObjectMapper.Map<PageViewDto>(page);
            }
            var list = new List<NameValue<int>>();
            foreach (DeviceType item in Enum.GetValues(typeof(DeviceType)))
            {
                list.Add(
                    new NameValue<int>(EnumHelper.GetEnumDescription(item), item.RawValue())
                    );
            }
            dto.DeviceTypes = list;
            return dto;
        }

        public async Task<PagedResultDto<PagedPageViewDto>> GetPagedPageViews(PagedPageViewInput input)
        {
             var query = _pageViewRepository.GetAll()
                .Where(c=>c.FrontEndPageId == input.FrontendPageId)
            .WhereIf(!input.Filter.IsNullOrEmpty(), t => t.Name.Contains(input.Filter));

            query = !input.Sorting.IsNullOrEmpty() ? query.OrderBy(input.Sorting) : query.OrderByDescending(t => t.CreationTime);

            var count = query.Count();
            var list = await query.PageBy(input).ToListAsync();
            return new PagedResultDto<PagedPageViewDto>(count, ObjectMapper.Map<List<PagedPageViewDto>>(list));
        }

        [AbpAuthorize(MenuPermissions.Pages_Administration_FrontendPageView_Edit)]
        public async Task SetStatus(SetStatusInput input)
        {
            await _pageViewManager.SetStatus(input.IdList, input.IsActive);
        }

        [AbpAuthorize(MenuPermissions.Pages_Administration_FrontendPageView_Create)]
        public async Task CopyViewAsync(CopyViewInput input)
        {
            await _pageViewManager.CopyViewAsync(input.ToFrontendPageId,input.IdList);
        }

        public async Task<List<NameValue>> GetFrontendPageListForCopyOp()
        {
            return await _pageViewManager.GetFrontendPageListForCopyOp();
        }
    }
}
